package com.study.zjh.smartrefreshdemo.activity;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.billy.android.swipe.SmartSwipeRefresh;
import com.billy.android.swipe.SmartSwipeWrapper;
import com.billy.android.swipe.SwipeConsumerExclusiveGroup;
import com.billy.android.swipe.consumer.SlidingConsumer;
import com.study.zjh.smartrefreshdemo.R;
import com.study.zjh.smartrefreshdemo.base.BaseActivity;
import com.study.zjh.smartrefreshdemo.view.SelfSmartSwipeHeader;
import com.study.zjh.smartrefreshdemo.view.SlefSmartSwipeFooter;

import java.util.ArrayList;
import java.util.List;

public class SmartSwipeListActivity extends BaseActivity {

    private ListView lv_data;
    private List<String>   list = new ArrayList<>();
    private SwipeConsumerExclusiveGroup exclusiveGroup = new SwipeConsumerExclusiveGroup();
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_swipe_list);
        lv_data =(ListView)findViewById(R.id.lv_data);
        initData();
        setRefresh();
        setadapter();
    }

    private void setRefresh(){
        SmartSwipeRefresh.SmartSwipeRefreshDataLoader loader = new SmartSwipeRefresh.SmartSwipeRefreshDataLoader() {
            @Override
            public void onRefresh(final SmartSwipeRefresh ssr) {
                list.clear();
                for(int i=0;i<10;i++){
                    list.add("onRefresh"+i+"个数据");
                }
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        setadapter();
                        //刷新完成后，如果数据不足一页，可以提前设置已加载完成的状态
                        ssr.setNoMoreData(false);
                        ssr.finished(true);//刷新成功
//                ssr.finished(false);//刷新失败
                    }
                },3000);

            }

            @Override
            public void onLoadMore(final SmartSwipeRefresh ssr) {
                for(int i=0;i<10;i++){
                    list.add("onLoadMore"+i+"个数据");
                }
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        setadapter();
                        if(list.size()>40){
                            ssr.setNoMoreData(true);
                        }
                        ssr.finished(true);
                    }
                },3000);

            }
        };
        //https://qibilly.com/SmartSwipe-tutorial/pages/SmartSwipeRefresh.html
        SmartSwipeRefresh.translateMode(lv_data, false)//false为纵向的  true为横向
                .setDataLoader(loader)
//                .startRefresh() //立即开启自动刷新
//                .startLoadMore()//自动加载更多
//                .disableRefresh()//禁用刷新功能
//                .disableLoadMore()//禁用加载更多
                .setHeader(new SelfSmartSwipeHeader(SmartSwipeListActivity.this))//自定义的头部
                .setFooter(new SlefSmartSwipeFooter(SmartSwipeListActivity.this))//自定义尾部
        ;
    }
    private void  initData(){
        for(int i=0;i<20;i++){
            list.add("第"+i+"个数据");
        }
    }

    private void setadapter(){
        if(adapter==null){
            adapter = new MyAdapter();
            lv_data.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
    }

    class MyAdapter extends BaseAdapter{
            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Object getItem(int position) {
                return list.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item2,null);
                SmartSwipeWrapper wrapper =(SmartSwipeWrapper)view. findViewById(R.id.ssw);
                wrapper.addConsumer(new SlidingConsumer()).setRelativeMoveFactor(SlidingConsumer.FACTOR_FOLLOW).addToExclusiveGroup(exclusiveGroup);
                Button button = (Button)view. findViewById(R.id.star_button);
                TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
                tv_name.setText(list.get(position));
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(SmartSwipeListActivity.this,"点击Button"+position,Toast.LENGTH_SHORT).show();
                        exclusiveGroup.markNoCurrent();
                        list.remove(position);
                        notifyDataSetChanged();
                    }
                });
                return view;
            }
        }
}
